package tgc.edu.exam.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.springframework.data.annotation.CreatedBy;

import com.fasterxml.jackson.annotation.JsonIgnore;

import tgc.edu.exam.custom.BaseEntity;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class SysUser extends BaseEntity<UUID>{
	private String username;
	private String password;
	private String name;
	private SysDept dept;		//部门
	private String openId;	
	
	@JsonIgnore
	private List<SysRole> roles=new ArrayList<>();
	
	
	@Column(length=64,unique=true)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(length=100)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(length=20)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name="sys_user_role",joinColumns=@JoinColumn(name="user_id"),inverseJoinColumns=@JoinColumn(name="role_id"))
	public List<SysRole> getRoles() {
		return roles;
	}
	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}
	@ManyToOne
	@CreatedBy
	public SysDept getDept() {
		return dept;
	}
	public void setDept(SysDept dept) {
		this.dept = dept;
	}
	public String getOpenId() {
		return openId;
	}
	public void setOpenId(String openId) {
		this.openId = openId;
	}
	@Transient
	public String getRoleNames() {
		String str="";
		for (SysRole role : getRoles()) {
			str+=role.getName()+",";
		}
		if(str.length()>0) {
			str=str.substring(0, str.length()-1);
		}
		return str;
	}
	
	@Transient
	public String getRoleCodes() {
		String str="";
		for (SysRole role : getRoles()) {
			str+=role.getCode()+",";
		}
		if(str.indexOf(",")>0) {
			str=str.substring(0,str.length()-1);
		}
		return str;
	}
	
}
